import numpy as np 

def sigmoid(x):
    return 1/(1+np.exp(-x))

def softmax(x):
    if x.ndim == 2:
        x = x - x.max(axis=1,keepdims=True)
        x = np.exp(x)
        x /= x.sum(axis=1,keepdims=True)
    elif x.ndim == 1:
        x = x -np.max(x)
        x = np.exp(x)/np.sum(np.exp(x))

    return x 

def cross_entropy_error(y,t):
    if y.ndim == 1:
        t = t.reshape(1,t.size)
        y = y.reshape(1,y.size)

    # 在监督标签为one-hot-vec的情况下，
    # 转换为正确解标签的的索引
    if t.size == y.size:
        t = t.argmax(axis=1)

    batch_size = y.shape[0]

    return -np.sum(np.log(y[np.arange(batch_size),t]+1e-7))/batch_size

if __name__ == '__main__':
    z=np.array([[1.0,2.0,3.0],[2.0,4.0,6.0]])
    print(softmax(z))